<?php
/**
 * Create by 11li
 * Date 2020/1/15
 * Time 15:38
 *
 */

namespace app\api\controller\v3;


use app\admin\model\User;
use app\common\controller\Api;
use app\common\model\AgentAdmin;
use app\common\model\AgentSendTips;
use app\common\model\AgentShop;
use app\common\model\PostMan;
use app\common\model\ShipOrder;
use app\common\service\AgentService;
use app\common\service\ShipService;
use think\Db;

class Ship extends Api
{
	
	/**
	 * 获取待发货提醒
	 */
	public function getWaitList()
	{
		$userId = $this->auth->id;
		$level = $this->auth->level;
		if ($level != User::LEVEL_HEADQUARTERS) {
			$agentId = ShipService::getAgentId($userId);
			if (!$agentId) {
				$this->success('没有权限', [
					'list' => [],
					'total' => 0
				]);
			}
			$agent = AgentShop::get($agentId);
			$userId = $agent['user_id'];
		}
		
		
		$list = ShipService::getSendList($userId);
		$this->success('查询成功', $list);
	}
	
	
	/**
	 * 获取发货记录
	 */
	public function getShipList()
	{
		$level = $this->auth->level;
		$agentId = '';
		if ($level != User::LEVEL_HEADQUARTERS) {
			$agentId = ShipService::getAgentId($this->auth->id);
			if ($agentId === false) {
				$this->success('没有权限', [
					'list' => [],
					'total' => 0
				]);
			}
			
			$agentId = $agentId === 0 ? '' : $agentId;
		}
		
		$postman = PostMan::get(['user_id' => $this->auth->id, 'status' => PostMan::STATUS_NORMAL]);
		$page = $this->request->get('page', 1, 'intval');
		$status = $this->request->get('status', ShipOrder::STATUS_WAIT, 'intval');
		
		$postmanId = $postman ? $postman['id'] : '';
		$res = ShipService::getShipRecord($agentId, $page, $status, $postmanId);
		$this->success('查询成功', $res);
	}
	
	/**
	 * 获取发货记录
	 */
	public function getShipInList()
	{
		$page = $this->request->get('page', 1, 'intval');
		$status = $this->request->get('status', 0, 'intval');
		$agentId = ShipService::getAgentId($this->auth->id);
		if (!$agentId) {
			$this->error('权限不足');
		}
		
		$model = new \app\common\model\ShipOrder();
		$orders = $model->getOrder($page, $agentId, '', '', $status);
		$this->success('查询成功', $orders);
	}
	
	/**
	 * 获取在售商品及代理库存
	 */
	public function getSaleGoods()
	{
		$goods = ShipService::getSaleGoodsAndSpec();
		
		if ($goods) {
			$agentId = ShipService::getAgentId($this->auth->id);
			if ($agentId) {
				$goods = ShipService::getAgentGoodsStock($goods, $agentId);
			}
		}
		$this->success('查询成功', $goods);
	}
	
	/**
	 * 代理及代理管理员的发货
	 */
	public function send()
	{
		//检测发货权限,重庆（战区，发展中心，提货点）,广东（宝能，火爆街）
		$userId = $this->auth->id;
		$level = $this->auth->level;
		
		$sendAgent = '';
		if ($level != User::LEVEL_HEADQUARTERS) {
			$agentId = ShipService::getAgentId($this->auth->id);
			if ($agentId === false) {
				$this->error('当前帐号没有发货权限');
			}
			
			if($agentId === 0) {
				$level = User::LEVEL_HEADQUARTERS;
			} else {
				$sendAgent = AgentShop::get($agentId);
				$level = $sendAgent['role'];
			}
		}
		
		$userModel = new User();
		if (!in_array($level,
			[$userModel::LEVEL_HEADQUARTERS, $userModel::LEVEL_CENTER, $userModel::LEVEL_WAREHOUSE])) {
			$this->error('当前帐号没有发货权限');
		}
		
		$postData = $this->request->post();
		$check = new \app\common\validate\ShipOrder();
		if (!$check->check($postData)) {
			$this->error($check->getError());
		}
		
		$numberList = $postData['number_list'];
		$destoryNumber = $postData['destroy_number'] ?? 0;
		if (!$numberList && !$destoryNumber) {
			$this->error('请输入发货数量或核销奖励数量');
		}
		
		$signAgent = AgentShop::get($postData['agent_id']);
		!$signAgent && $this->error('收货下级不存在');
		
		//重庆要选择配送员
//		if ($sendAgent && $sendAgent['area'] == User::AREA_OTHER && empty($postData['postman_id'])) {
//			$this->error('请选择配送员');
//		}
		
		$model = new \app\common\model\ShipOrder();
		$postmanId = $postData['postman_id'] ?? '';
		$res = $model->sendGoods($signAgent['area'], $postData['ship_name'], $postData['ship_no'], $numberList,
			$signAgent, $sendAgent, $postmanId, $destoryNumber);
		
		$tipsRes = Db::name('agentSendTips')
			->where(['user_id' => $userId, 'agent_id' => $signAgent['id']])
			->update(['status' => AgentSendTips::STATUS_DONE, 'update_time' => time()]);
		
		if ($res) {
			$this->success('发货成功');
		} else {
			$this->error($model->getError());
		}
		
		
	}
	
	/**
	 * 关闭订单
	 */
	public function close() {
		$orderId = $this->request->get('order_id', '', 'intval');
		!$orderId && $this->error('参数错误');
		
		if($this->auth->level == User::LEVEL_HEADQUARTERS) {
			$agentId = 0;
		} else {
			$agentId = ShipService::getAgentId($this->auth->id);
			if ($agentId === false) {
				$this->error('权限不足');
			}
		}
		
		
		$service = new ShipService();
		$res = $service->closeOrder($agentId, $orderId);
		
		if($res) {
			$this->success('关闭成功');
		} else {
			$this->error($service->getError());
		}
	}
}